package com.hisicom.scqxspxcjc.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.hisicom.scqxspxcjc.base.bean.Result;
import com.hisicom.scqxspxcjc.common.file.bean.FileBean;
import com.hisicom.scqxspxcjc.common.file.exception.FileException;
import com.hisicom.scqxspxcjc.common.file.util.GlobalType;
import com.hisicom.scqxspxcjc.sceneCheck.systemCheck.domain.SystemCheck;
import com.hisicom.scqxspxcjc.services.SystemCheckServiceBus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author zhonggls
 * @date 2023/9/27 09:22
 * @Explain
 */
@RestController
@RequestMapping("sceneCheck/systemCheck")
public class SystemCheckController {
    @Autowired
    SystemCheckServiceBus systemCheckServiceBus;
    @Value("${file.basePath}")
    String basePath;

    /**
     * 通过账号名获取对应体系核查列表
     *
     * @param account 账号名称
     * @return 体系核查集合
     */
    @GetMapping(value = "getList")
    public Object getList(String account) {
        List<SystemCheck> systemCheckList = systemCheckServiceBus.findAllSystemCheckList(account);
        return Result.success(systemCheckList);
    }

    /**
     * 通过组id获取体系核查对象集合
     *
     * @param groupId 分组id
     * @return 体系核查对象集合
     */
    @GetMapping(value = "getListByGroupId")
    public Object getListByGroupId(String groupId) {
        List<SystemCheck> systemCheckList = systemCheckServiceBus.findAllSystemCheckByGroupId(groupId);
        return Result.success(systemCheckList);
    }

    /**
     * 通过组id获取体系核查对象集合
     *
     * @param groupId 分组id
     * @return 体系核查对象集合
     */
    @GetMapping(value = "updateByGroupId")
    public Object updateByGroupId(String groupId) {
        if (StrUtil.isNotBlank(groupId)) {
            systemCheckServiceBus.updateCheckStatus(groupId.split(","), "1");
            return Result.success();
        } else {
            return Result.error("分组ID不能为空");
        }
    }
   /**
     * 提交检查
     *
     * @param groupId 分组id
     * @return 体系核查对象集合
     */
    @GetMapping(value = "submit")
    public Object submit(String groupId) {
        if (StrUtil.isNotBlank(groupId)) {
            systemCheckServiceBus.submit(groupId);
            return Result.success();
        } else {
            return Result.error("分组ID不能为空");
        }
    }

    /**
     * 保存文件
     *
     * @param groupId 分组id
     * @param file    文件
     * @param label   类型 schc:首次核查 ;zghc:整改复查; xcjcjl:现场检查记录 ;bfh:不符合项目情况表;
     * @return
     */
    @PostMapping("file/save")
    public Result save(String groupId, @RequestParam(value = "file", required = false) MultipartFile file, String label) {
        if (ObjectUtil.isEmpty(file)) {
            throw new FileException("上传文件时，文件不能为空");
        }
        String originalFilename = file.getOriginalFilename();
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        if (StrUtil.isBlank(substring)) {
            throw new FileException("上传文件时，文件不能为空");
        }
        systemCheckServiceBus.save(groupId, file, label, originalFilename);
        return Result.success();
    }

    /**
     * 保存文件
     *
     * @param groupId 分组id
     * @param file    文件
     * @param label   类型 schc:首次核查 ;zghc:整改复查; xcjcjl:现场检查记录 ;bfh:不符合项目情况表;
     * @return
     */
    @GetMapping("file/getFileByGropuId")
    public Result getFileByGropuId(String groupId, String label) {
        if (StrUtil.isEmpty(groupId)) {
            throw new FileException("查找文件时，groupId不能为空");
        }
        return Result.success(systemCheckServiceBus.findFileByGroupId(groupId, label));
    }

    /**
     * 删除文件
     *
     * @param id 分组id
     * @return
     */
    @GetMapping("file/delById")
    public Result delById(String id) {
        if (StrUtil.isEmpty(id)) {
            throw new FileException("删除文件时，Id不能为空");
        }
        systemCheckServiceBus.delById(id);
        return Result.success();
    }

    /**
     * 查看文件
     *
     * @param f
     * @param response
     */
    @GetMapping("/file/getFile")
    public void getFile2(String f, HttpServletResponse response) {
        if (f == null || f.equals("")) {
            return;
        }
        String suffix = f.split("\\.")[1];
        if (GlobalType.PDF_FILE.equals(suffix)) {
            response.setContentType("application/pdf");
        } else if (GlobalType.JPEG.equals(suffix) || GlobalType.JPG.equals(suffix)) {
            response.setContentType("image/jpeg");
        } else if (GlobalType.PNG.equals(suffix)) {
            response.setContentType("image/png");
        } else if (GlobalType.DOC.equals(suffix) || GlobalType.DOCX.equals(suffix)) {
            response.setContentType("application/msword");
        } else if (GlobalType.XLSX.equals(suffix)) {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        } else if (GlobalType.XLS.equals(suffix)) {
            response.setContentType("application/application/vnd.ms-excel");
        }
        File file;
        if (GlobalType.OFD_FILE.equals(suffix)) {
            file = new File(f);
        } else {
            if (f.contains(basePath)) {
                file = new File(f);
            } else {
                file = new File(basePath + f);
            }
        }
        try {
            FileCopyUtils.copy(new FileInputStream(file), response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/file/getFileById")
    public void getFileById(String id, HttpServletResponse response) {
        if (StrUtil.isBlank(id)) {
            return;
        }
        FileBean fileBean = systemCheckServiceBus.findByFileId(id);
        String path = fileBean.getPath();
        String suffix = path.split("\\.")[1];
        if (GlobalType.PDF_FILE.equals(suffix)) {
            response.setContentType("application/pdf");
        } else if (GlobalType.JPEG.equals(suffix) || GlobalType.JPG.equals(suffix)) {
            response.setContentType("image/jpeg");
        } else if (GlobalType.PNG.equals(suffix)) {
            response.setContentType("image/png");
        } else if (GlobalType.DOC.equals(suffix) || GlobalType.DOCX.equals(suffix)) {
            response.setContentType("application/msword");
        } else if (GlobalType.XLSX.equals(suffix)) {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        } else if (GlobalType.XLS.equals(suffix)) {
            response.setContentType("application/application/vnd.ms-excel");
        }
        File file;
        if (GlobalType.OFD_FILE.equals(suffix)) {
            file = new File(path);
        } else {
            if (path.contains(basePath)) {
                file = new File(path);
            } else {
                String pathLong = basePath + path;
                file = new File(pathLong);
            }
        }
        try {
            FileCopyUtils.copy(new FileInputStream(file), response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
